package sum.book.common.web.filter;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: ZhangGN
 * @Description:
 * @Date 2023/1/4 9:47
 **/
public class HeaderValueFilter extends OncePerRequestFilter {
    private static final String HEADER_INTERIOR = "INTERIOR";

    private static final String HEADER_VALUE = "TRUE";

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String headerValue = request.getHeader(HEADER_INTERIOR);
        // 如果 是内部接口 则直接跳过后续的过滤器,直接调用接口
        if (StringUtils.equals(HEADER_VALUE, headerValue)) {
            String requestUrl = request.getRequestURI();
            String substring = requestUrl.substring(requestUrl.indexOf("/", 1));
            // String substring = requestUrl.substring(requestUrl.indexOf("/"));
            // 直接调用业务接口
            request.getRequestDispatcher(substring).forward(request, response);
        } else {
            // 如果不是的 则 直接放行 走后续的认证操作
            filterChain.doFilter(request, response);
        }
    }
}
